<?php
namespace app\shop\model;

use app\BaseModel;
use app\farm\model\ShopGoods;
use think\facade\Db;

class SeckillList extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_v2_seckill_list';


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 15:43
     * @功能说明:检查活动时间
     */
    public function atvCheck($input){

        $dis[] = ['uniacid','=',$input['uniacid']];

        $dis[] = ['status','>',-1];

        if(!empty($input['id'])){

            $dis[] = ['id','<>',$input['id']];

        }

        $list = $this->where($dis)->select()->toArray();

        if(!empty($list)){

            foreach ($list as $value){

                $res = is_time_cross($input['start_time'],$input['end_time'],$value['start_time'],$value['end_time']);

                if($res==false){

                    return ['code'=>500,'msg'=>'该时间段已经有该活动，活动名称:'.$value['title']];
                }

            }
        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @param $id
     * @param $uniacid
     * @param $config
     * @param $store
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-15 16:11
     */
    public function updateSome($id,$uniacid,$spe,$goods_id){

       $config_model = new SeckillConfig();

       $goods_model   = new SeckillGoods();

       $insert = [

           'uniacid' => $uniacid,

           'atv_id'  => $id,

           'goods_id'=> $goods_id
       ];

       $find = $goods_model->dataInfo($insert);

       if(empty($find)){

           $goods_model->dataAdd($insert);

           $atv_id = $goods_model->getLastInsID();

       }else{

           $atv_id = $find['id'];

       }

       if(!empty($spe)){

            foreach ($spe as $value){

                $i_dis = [

                    'atv_id'  => $id,

                    'goods_id'=> $goods_id,

                    'spe_id'  => $value['spe_id'],
                ];

                $have_stock = $config_model->where($i_dis)->sum('have_stock');

                $value['uniacid'] = $uniacid;

                $value['atv_id']  = $atv_id;

                $value['have_stock'] = $have_stock;

                $config_model->insert($value);

                $arr[] = $config_model->getLastInsID();
            }

       }

        $config_model->where(['atv_id'=>$atv_id])->where('id','not in',$arr)->delete();

       return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }




    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:54
     * @功能说明:初始化活动时间
     */
    public function initAtv(){

        $this->where('start_time','>',time())->update(['atv_status'=>1]);

        $this->where('start_time','<',time())->update(['atv_status'=>2]);

        $this->where('end_time','<',time())->update(['atv_status'=>3]);

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-01 10:18
     * @功能说明:正在进行中的活动
     */
    public function atvIng($goods_id,$spe_id=0,$type=1){

        $this->initAtv();

        if($type==1){

            $dis[] = ['a.atv_status','=',2];

        }else{

            $dis[] = ['a.atv_status','in',[1,2]];

        }

        $dis[] = ['b.goods_id','=',$goods_id];

        $dis[] = ['a.status','=',1];

        if(!empty($spe_id)){

            $dis[] = ['c.spe_id','=',$spe_id];
        }

//        $dis[] = ['c.store_id','=',$store_id];
//
//        $dis[] = ['c.type','=',6];

        $data = $this->alias('a')
            ->join('lbfarm_v2_seckill_goods b','a.id = b.atv_id')
            ->join('lbfarm_v2_seckill_config c','b.id = c.atv_id')
           // ->join('lbfarm_v2_goods_store c','a.id = c.goods_id')
            ->where($dis)
            ->field('b.*,a.*,c.price,c.spe_id,c.stock,c.have_stock,a.id as kill_atv_id')
            ->order('c.price')
            ->find();

        if(!empty($data)){

            $data = $data->toArray();
            //减去已经销售的
            $data['stock'] = ($data['stock'] - $data['have_stock'])>0?$data['stock'] - $data['have_stock']:0;

            $spe_model = new GoodsSpePrice();

            $data['init_price'] = $spe_model->where(['id'=>$data['spe_id']])->value('price');

        }

        return $data;

    }





    /**
     * @author chenniang
     * @DataTime: 2021-11-04 14:41
     * @功能说明:获取是否
     */
    public function getBuyLimit($list,$store_id=0){

        foreach ($list as $value){

            $res = $this->atvIng($value['goods_id'],$value['spe_id']);

            if(!empty($res)){

                return 1;

            }

        }

        return 0;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-28 15:58
     * @功能说明:校验用户是否可以参加秒杀
     */
    public function userCheck($user_id,$atv_id,$is_show){

        if(empty($atv_id)){

            return true;
        }

        if(!empty($atv_id)&&$is_show!=2){

            return ['code'=>500,'msg'=>'秒杀商品不能从购物车下单哦'];
        }

        $atv = $this->dataInfo(['id'=>$atv_id]);

        if(empty($atv)){

            return ['code'=>500,'msg'=>'秒杀活动已结束'];

        }

        if($atv['is_limit']==1){

            $num = $this->getAtvNum($atv_id,$user_id);

            if($num>=$atv['limit_num']){

                return ['code'=>500,'msg'=>'超过秒杀次数'];

            }

        }

        return true;

    }


    /**
     * @param $dis
     * @return float
     * 获取历史商品件数
     */
    public function getAtvNum($id,$user_id){

        $dis = [

            'b.kill_atv_id' => $id,

            'b.user_id'     => $user_id

        ];

        $order_model = new \app\farm\model\ShopOrderGoods();
        //取消订单的不算 chen
        $num = $order_model->alias('a')
            ->join('lbfarm_shop_order b','a.order_id = b.id')
            ->where($dis)
            ->where('b.pay_type','>=',1)
            ->group('a.id')
            ->sum('a.goods_num');


        $order_id = $order_model->alias('a')
            ->join('lbfarm_shop_order b','a.order_id = b.id')
            ->where($dis)
            ->where('b.pay_type','>=',1)
            ->column('b.id');

        $order_refund_goods = new \app\farm\model\ShopRefundGoods();
        //申请退款成功的
        $refund_num = $order_refund_goods->where('order_id','in',$order_id)->where(['status'=>2])->sum('goods_num');

        return $num - $refund_num;

    }








}